<template>
  <chart :options="options" :auto-resize="autoResize" :theme="theme" :watch-shallow="watchShallow"
      :class="className" :style="{height:height,width:width}"></chart>
</template>

<script>

const animationDuration = 2000

function getCharLength(str) {
  return str.replace(/[^\x00-\xff]/g, 'rr').length
}
function subStr(str, charLength) {
  if (getCharLength(str) <= charLength) return str
  var m = Math.floor(charLength / 2)
  for (var i = m; i < str.length; i++) {
    if (getCharLength(str.substr(0, i)) >= charLength) {
      return str.substr(0, i)
    }
  }
  return str
}

export default {
  props: {
    chartData: { type: Object },
    className: { type: String, default: 'chart' },
    theme: { type: String, default: 'macarons' },
    autoResize: { type: Boolean, default: true },
    watchShallow: { type: Boolean, default: true },
    width: { type: String, default: '100%' },
    height: { type: String, default: '300px' }
  },
  data() {
    return {
      options: this.buildOptions(this.charData)
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.options = this.buildOptions(val)
      }
    }
  },
  mounted() {
    if (!this.options && this.chartData) {
      this.options = this.buildOptions(this.chartData)
    }
  },
  methods: {
    buildOptions(chartData) {
      var colorList = ['#2ec7c9', '#b6a2de', '#5ab1ef', '#ffb980', '#d87a80', '#8d98b3', '#e5cf0d', '#97b552', '#95706d', '#dc69aa', '#07a2a4', '#9a7fd1', '#588dd5', '#f5994e', '#c05050', '#59678c', '#c9ab00', '#7eb00a', '#6f5553', '#c14089']
      // console.log(chartData)
      if (!chartData || !chartData.valueBeans) {
        return
      }
      var category = chartData.category
      var seriesData = chartData.valueBeans.map(item => {
        return {
          name: item.name,
          type: 'bar',
          data: item.data,
          itemStyle: {
            normal: {
              // 随机显示
              // color: function(d) { return '#' + Math.floor(Math.random() * (256 * 256 * 256 - 1)).toString(16) }
              // 定制显示（按顺序）
              color: function(params) { return colorList[params.dataIndex % colorList.length] }
            }
          }
        }
      })

      return {
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: 10,
          left: '5%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: category,
          axisTick: {
            alignWithLabel: true
          },
          axisLabel: {
            interval: 0,
            rotate: 45,
            formatter: function(value) {
              var res = value
              if (getCharLength(res) > 10) {
                res = subStr(res, 8) + '..'
              }
              return res
            }
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          }
        }],
        series: seriesData,
        animationDuration
      }
      /* [{
          name: 'pageA',
          type: 'bar',
          data: [79, 52, 200, 334, 390, 330, 220],
          animationDuration
        }] */
    }
  }
}
</script>
